﻿@model Mestedur.ViewModels.EditorViewModels.EditorQuestionViewModel 

@{
    ViewBag.Title = "Редактирование вопроса";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Редактирование вопроса</h2>

<form method="post">
<input type = "hidden" name = "id" value = "@Model.Id">
<table>
  <tr>
    <td>@Html.LabelFor(x=>x.ChapterId)</td>
    <td>@Html.DropDownListFor(x => x.ChapterId, new SelectList(ViewBag.Chapters, "Id", "ChapterName", Model.ChapterId))</td>
  </tr>
  <tr>
    <td>@Html.LabelFor(x=>x.QuestionMedia)</td>
    <td>@Html.TextAreaFor(x => x.QuestionMedia)</td>
  </tr>
</table>
<div>
    <input id = "radio_one" type="radio" name="QuestionType" value="one" @if (Model.Answers.Count == 1) 
                                                   { 
                                                       @:checked = "checked"
                                                   } > Письменный ответ<br/>
    <input id = "radio_many" type="radio" name="QuestionType" value="many" @if (Model.Answers.Count != 1) { 
                                                       @:checked = "checked"
                                                   }> Несколько ответов<br/>
    <div id = "single_answer_edit_layer" @if (Model.Answers.Count != 1) 
                                         {
                                             @: style = "display:none;"
                                         }>
        <input type = "text" name = "textanswer" value = "@Model.QuestionMedia"/>
    </div>
    <div id = "many_answer_edit_layer" @if (Model.Answers.Count == 1) 
                                         {
                                             @: style = "display:none;"
                                         }>
        <ul id = "answers_list"> 
        @{ViewBag.index = 0;}
        @foreach (var item in Model.Answers)
        {
            ViewBag.index++;
            <li>
                <input type = "hidden" name = "AnswerId" value = "@ViewBag.index"/>
                <input id="AnswerText" name="Answers" type = "text" value="@item.AnswerText" />
                <input id="AnswerRight" name="AnswerRight" type = "checkbox" value="@ViewBag.index" @if (item.Correctness)
                                                                                                   { 
                                                                                                       @:checked = "checked"
                                                                                                   }/>
                <input type = "button" class = "delete_question" value = "Удалить ответ"/>
            </li> 
        }
        

<script type="text/javascript">
    var index = @ViewBag.index+1;
    var deleteItem = function (handle) {
        $(handle).parent("li").remove();
    }
    $(document).ready(function () {
        $("#radio_one").click(function () {
            $("#single_answer_edit_layer").show();
            $("#many_answer_edit_layer").hide();
        });
        $("#radio_many").click(function () {
            $("#single_answer_edit_layer").hide();
            $("#many_answer_edit_layer").show();
        });
        $("#add_question").click(function () {
            var newListItem = $('<li> <input type = "hidden" name = "AnswerId" value = "' + index + '"/>'+
                '<input id="AnswerText" name="Answers" type = "text" valule = ""/>' +
                '<input id="AnswerRight" name="AnswerRight" type = "checkbox" value="' + index + '"/>' +
                '<input type = "button" class = "delete_question" value = "Удалить ответ"/></li>');
            newListItem.appendTo("#answers_list");
            newListItem.find(".delete_question").click(function () { deleteItem(this); });
            index = index + 1;
        });
        $(".delete_question").click(function () { deleteItem(this); });
    });
</script>        
        </ul>
        <input type = "button" id = "add_question" value = "Добавить ответ"/>
        </div>
</div>
<hr/>
<input type="submit" value="Сохранить" />
</form>
